﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_Policy")]
    public class T_Policy : IEntity<T_Policy>
    {
        [Key]
        [Column(TypeName="int")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public int ProductId { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string ProductName { get; set; }
        public decimal Price { get; set; }
        public int PolicyType { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime ExpireDate { get; set; }
        public bool IsActive { get; set; }
        public DateTime CreateDate { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string Creator { get; set; }

        public override T_Policy Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Policy Find(System.Linq.Expressions.Expression<Func<T_Policy, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Policy.FirstOrDefault(where);
                if (item != null)
                {
                    this.CreateDate = item.CreateDate;
                    this.Creator = item.Creator;
                    this.ExpireDate = item.ExpireDate;
                    this.Id = item.Id;
                    this.IsActive = item.IsActive;
                    this.PolicyType = item.PolicyType;
                    this.Price = item.Price;
                    this.ProductId = item.ProductId;
                    this.ProductName = item.ProductName;
                    this.StartDate = item.StartDate;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Policy, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Policy.Any(where);
            }
        }

        public override T_Policy FindLast(System.Linq.Expressions.Expression<Func<T_Policy, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Policy.OrderByDescending(c=>c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.CreateDate = item.CreateDate;
                    this.Creator = item.Creator;
                    this.ExpireDate = item.ExpireDate;
                    this.Id = item.Id;
                    this.IsActive = item.IsActive;
                    this.PolicyType = item.PolicyType;
                    this.Price = item.Price;
                    this.ProductId = item.ProductId;
                    this.ProductName = item.ProductName;
                    this.StartDate = item.StartDate;
                    return this;
                }
                return null;
            }
        }
    }
}
